﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IntraVision.Web;

namespace Model
{
    public partial class Person : IUser, IClient
    {
        #region IUser Members
        public bool HasPermission(string role)
        {
            if (role == "IsAdmin") return false;
            return true;
        }
        string IUser.Name
        {
            get { return FIO; }
        }

        #endregion

        #region IClient Members
        string IClient.Name
        {
            get { return this.FullName; }
        }
        public string IdStr
        {
            get { return String.Format("p{0}", this.Id); }
        }
        public string TypeName
        {
            get { return "person"; }
        }
        public int Cid
        {
            get { return this.Id * 10 + 1; }
        }
        public bool IsCashless
        {
            get { return false; }
        }
        #endregion

        public string FIO
        {
            get { return String.Format("{0} {1} {2}", this.LastName, this.Name, this.PatronymicName); }
        }

        public string FullName
        {
            get { return string.Format("{0} {1} {2}"
                    , LastName
                    , (Name != null && string.Empty != Name.Trim() ? Name[0] + "." : "")
                    , (PatronymicName != null && string.Empty != PatronymicName.Trim() ? PatronymicName[0] + "." : "")); }
        }

        public string[] PhonePartsArray
        {
            get
            {
                string[] result = new string[4];

                if (!string.IsNullOrEmpty(this.Phone))
                {
                    try
                    {
                        result[0] = this.Phone.Substring(3, 3);
                        result[1] = this.Phone.Substring(7, 3);
                        result[2] = this.Phone.Substring(11, 2);
                        result[3] = this.Phone.Substring(14, 2);
                    }
                    catch
                    {
                        result = new string[4];
                    }                 
                }
                return result;
            }
            set
            {
                if (value != null && value.Length == 4)
                    this.Phone = String.Format("+7({0}){1}-{2}-{3}", value);
                else
                    throw new ArgumentException("Телефон на задан или имеет неправильный формат.");
            }
        }

        public string ToUnionLink()
        {
            return string.Format("<a href=\"person.ivp/union/{0}\">{1}</a>", this.Id, this.FIO);
        }
    }
}
